const chalk = require('chalk');

const {textIndent, group,selectorWrap} = require('../config/style');
const {STYLE_UNIT_TYPE, sort} = require('./const');
const styleUnitToASTObj = require('./styleUnitToASTObj');
const hh = '\r\n';

const toStyle = ({rule}, indent2 = '') => {
    return rule.map((item, index) => {
        return `${textIndent}${indent2}${Object.keys(item)[0]}: ${item[Object.keys(item)[0]].text};${index === rule.length - 1 ? '' : hh}${group && (index !== rule.length - 1) && (item && item[Object.keys(item)[0]].ownerStaged) !== (rule[index + 1] && rule[index + 1][Object.keys(rule[index + 1])[0]].ownerStaged) ? hh : ''}`
    }).join('')
};

const toSelector=x=>{
    return selectorWrap?x.split(',').join(','+hh):x
};
const toChildStyle = (childRule) => {
    return childRule.map((x, index) => {
        return `${textIndent}${toSelector(x.selector)}{${hh}${toStyle(x, textIndent)}${hh}${textIndent}}${index === childRule.length - 1 ? '' : hh}`;
    }).join(hh)
};
const cssASTToText = (styleSheetUnit) => {
    return styleSheetUnit.map((x, index, array) => {
        if (x.type === STYLE_UNIT_TYPE.CHARSET || x.type === STYLE_UNIT_TYPE.IMPORT || x.type === STYLE_UNIT_TYPE.COMMENT) {
            return `${x.text}${hh}${index < array.length - 1 && array[index + 1].type === x.type ? '' : hh}`
        }
        if (x.type === STYLE_UNIT_TYPE.ORDINARY || x.type === STYLE_UNIT_TYPE.FONT_FACE || x.type === STYLE_UNIT_TYPE.PACE) {
            return `${toSelector(x.selector)} {${hh}${toStyle(x)}${hh}}${hh}${hh}`;
        }
        return `${x.ruleName} {${hh}${toChildStyle(x.childRule)}${hh}}${hh}${index < array.length - 1 && array[index + 1].type === x.type ? '' : hh}`;
    }).join('')
};

const getStyleSheetUnitTextType = (styleSheetUnitText = '') => {
    if (/^@charset/.test(styleSheetUnitText)) {
        return STYLE_UNIT_TYPE.CHARSET
    }
    if (/^@import/.test(styleSheetUnitText)) {
        return STYLE_UNIT_TYPE.IMPORT
    }
    if (/^@media/.test(styleSheetUnitText)) {
        return STYLE_UNIT_TYPE.MEDIA
    }
    if (/^@font-face/.test(styleSheetUnitText)) {
        return STYLE_UNIT_TYPE.FONT_FACE
    }
    if (/^@page/.test(styleSheetUnitText)) {
        return STYLE_UNIT_TYPE.PACE
    }
    if (/^@keyframes/.test(styleSheetUnitText)) {
        return STYLE_UNIT_TYPE.KEY_FRAMES
    }
    if (/^\/\*((?!\*\/)(.|\r\n|\n))*?\*\/$/.test(styleSheetUnitText)) {
        return STYLE_UNIT_TYPE.COMMENT
    }
    return STYLE_UNIT_TYPE.ORDINARY
};
const textToCssAST = (text) => {
    const sliceStyleUnitReg = /((@import([^;])+?;)|(@charset([^;])+?;)|((@media|@keyframes)([^\{\}])*?\{(([^\{\}])*?\{([^\}\{])*?\})+?([^\}\{])*?\})|(\/\*((?!\*\/)|(.|\r\n|\n))*?\*\/)|((.|\r\n|\n)+?\{([^\{\}])+?\}))/ig
    const styleSheetUnit = text.match(sliceStyleUnitReg);
    console.error('00', styleSheetUnit);
    const styleSheetUnit2 = (styleSheetUnit || []).map(text => {
        let styleSheetUnitText = text.replace(/\r\n|\n/g, '');
        const styleSheetUnitType = getStyleSheetUnitTextType(styleSheetUnitText);
        if (styleSheetUnitType === STYLE_UNIT_TYPE.COMMENT) {
            styleSheetUnitText = text
        }
        return Object.assign(styleUnitToASTObj[styleSheetUnitType](styleSheetUnitText), {type: styleSheetUnitType});
    });
    console.error(444, JSON.stringify(styleSheetUnit2))
    return styleSheetUnit2
};

const {sortArray} = require('../config/sort');

const map = sortArray.reduce((pre, cur, index) => {
    return {
        ...pre,
        [cur]: index
    }
}, {});

const map2 = sort.reduce((pre, cur, index) => {
    return {
        ...pre,
        [cur]: index
    }
}, {});

const sortFunc = (a, b) => {
    if (map[Object.keys(a)[0]] === undefined) {
        console.log(chalk.red('WARNING 请在sort.js中定义:', chalk.underline.red(Object.keys(a)[0]), '属性的顺序'))
    }
    if (map[Object.keys(a)[0]] > map[Object.keys(b)[0]]) {
        return 1;
    }
    return -1
};
module.exports.cssASTToText = cssASTToText;
module.exports.textToCssAST = textToCssAST;
